package com.mynovamobile.android;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.app.Activity;
import android.provider.Settings.Secure;
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONException;
import org.json.JSONObject;

public class MNMRequest {

	private final static String TAG = "MNMRequest";

	private ArrayList<BasicNameValuePair> postData;
	private Activity sourceActivity;
	private DefaultHttpClient httpclient;
	private HttpPost httppost;
	private BasicResponseHandler responseHandler;

	public MNMRequest(Activity sourceActivity) {
		this.sourceActivity = sourceActivity;
		postData = new ArrayList<BasicNameValuePair>();
		this.addData("UDID", Secure.getString(sourceActivity
				.getContentResolver(), Secure.ANDROID_ID));
	}

	public void addData(String key, String value) {
		postData.add(new BasicNameValuePair(key, value));
	}

	public JSONObject executeRequest(String url)
			throws ClientProtocolException, UnsupportedEncodingException,
			IOException, JSONException {
		httpclient = new DefaultHttpClient();
		httppost = new HttpPost(url);
		responseHandler = new BasicResponseHandler();

		httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(postData));
		String response = httpclient.execute(httppost, responseHandler);
		JSONObject jsonResponse = new JSONObject(response);
		return jsonResponse;
	}

	public ArrayList<BasicNameValuePair> getKeyValuePairs() {
		return postData;
	}
}